package com.fyzk.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.newJz.data.NewScoreDetailActivity;
import com.fyzk.bean.JCLQResponseInfo;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.utils.ButtonUtils;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * 胜分差adapter
 * 
 * @author fxs
 * 
 */
public class JCLQSFCAdapter extends JCLQBaseAdapter {

	public JCLQSFCAdapter(Activity activity, List<JCLQResponseInfo> infos) {
		super(activity, infos);
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final JCLQMatchData data = infos.get(groupPosition).matchDatas
				.get(childPosition);
		final ChildHolder childHolder;
		if (convertView == null) {
			convertView = miInflater.inflate(R.layout.jclq_adapter_child_sfc,
					null);
			childHolder = new ChildHolder();
			childHolder.xianShi = convertView.findViewById(R.id
					.jc_main_list_item_xianshi);
			childHolder.ll_xiangqing = convertView.findViewById(R.id
					.jc_main_list_item_yincang);
			childHolder.league_tv = convertView
					.findViewById(R.id.jclq_child_base_league);
			childHolder.teamId_tv = convertView
					.findViewById(R.id.jclq_child_base_teamid);
			childHolder.time_tv = convertView
					.findViewById(R.id.jclq_child_base_time);
			childHolder.right_ll = convertView
					.findViewById(R.id.jclq_child_base_right);
			childHolder.home_tv = convertView
					.findViewById(R.id.jclq_child_sfc_home);
			childHolder.away_tv = convertView
					.findViewById(R.id.jclq_child_sfc_away);
			childHolder.zhanKai_check = convertView
					.findViewById(R.id.jclq_child_sfc_zhankai);

			// childHolder.home_check = (CheckBox) convertView
			// .findViewById(R.id.jclq_child_sf_home);
			// childHolder.away_check = (CheckBox) convertView
			// .findViewById(R.id.jclq_child_sf_away);

			convertView.setTag(childHolder);
		} else {
			childHolder = (ChildHolder) convertView.getTag();
		}

		childHolder.league_tv.setText(data.getMatchName());
		try {
			String teamId = data.getMatchNum().substring(data.getMatchNum().length()-3,data.getMatchNum().length());
			childHolder.teamId_tv.setText(data.getOfficialWeekDay() + teamId);
			String endData = new SimpleDateFormat("HH:mm").format(data.getEndSaleTime());
			childHolder.time_tv.setText(endData + "截止");
		} catch (Exception e) {

		}
		childHolder.home_tv.setText(data.getHomeTeam()+"[主]");
		childHolder.away_tv.setText(data.getAwayTeam()+"[客]");
		childHolder.zhanKai_check.setChecked(data.clickNum>0);
		childHolder.zhanKai_check.setText(data.getSFCResult());
		childHolder.zhanKai_check.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				changCi = 0;
				if (data.hideView != null) {
					data.hideView = null;
					data.isAddhideView = false;
				}
				for (int i = 0; i < infos.size(); i++) {
					for (int j = 0; j < infos.get(i).matchDatas.size(); j++) {
						JCLQMatchData data = infos.get(i).matchDatas.get(j);
						if (data.clickNum > 0) {
							changCi++;
						}
					}
				}
				if (data.clickNum == 0 && changCi == maxCount) {
					AppToast.getToast().show("最多可选"+maxCount+"场比赛");
					childHolder.zhanKai_check.setChecked(false);
				} else {

					if (ButtonUtils.isFastDoubleClick(500)) {
						return;
					}
					((CheckBox)v).setChecked(data.clickNum>0);
					data.sfc_isZhanKai = true;
					if(popOpenListener != null){
						popOpenListener.open(data);
					}
				}
			}
		});

		if (data.isShowHideView) {
			childHolder.ll_xiangqing.setVisibility(View.VISIBLE);
		} else {
			childHolder.ll_xiangqing.setVisibility(View.GONE);
		}
		if (data.hideView != null) {
			ViewGroup parent1 = (ViewGroup) data.hideView.getParent();
			if (parent1 != null) {
				parent1.removeAllViews();
			}
			childHolder.ll_xiangqing.removeAllViews();
			childHolder.ll_xiangqing.addView(data.hideView);
		}

		// 点击显示隐藏信息
		childHolder.xianShi.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

//				if (!data.isAddhideView) {
//					data.hideView = new BasketballMatchSummaryView(context, data.getMatchNum());
//					childHolder.ll_xiangqing.removeAllViews();
//					childHolder.ll_xiangqing.addView(data.hideView);
//					data.isAddhideView = true;
//				}
//				data.isShowHideView = !data.isShowHideView;
//				if (data.isShowHideView) {
//					childHolder.ll_xiangqing.setVisibility(View.VISIBLE);
//				} else {
//					childHolder.ll_xiangqing.setVisibility(View.GONE);
//				}
				Intent intent = new Intent(context, NewScoreDetailActivity.class);
				intent.putExtra("isFootball", false);
				intent.putExtra("matchNum", data.getMatchNum());
				context.startActivity(intent);
			}

		});
//		childHolder.ll_xiangqing.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				Intent intent = new Intent(context, BasketballScoreDetailActivity.class);
//				intent.putExtra("matchNum", data.getMatchNum());
//				context.startActivity(intent);
//			}
//		});

		return convertView;
	}

	class ChildHolder {
		TextView league_tv;
		TextView teamId_tv;
		TextView time_tv;
		LinearLayout right_ll;
		// CheckBox home_check;
		// CheckBox away_check;
		TextView home_tv;
		TextView away_tv;
		CheckBox zhanKai_check;
		LinearLayout xianShi;
		LinearLayout ll_xiangqing;

	}


}
